//
//  LGKNotePhotoManager.m
//  AIEnglishTeaching
//
//  Created by 刘亚军 on 2021/9/8.
//  Copyright © 2021 刘亚军. All rights reserved.
//

#import "LGKNotePhotoManager.h"
@interface LGKNotePhotoManager ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>

@end
@implementation LGKNotePhotoManager
+ (instancetype)manage{
    static LGKNotePhotoManager * macro = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        macro = [[LGKNotePhotoManager alloc]init];
        [macro configure];
    });
    return macro;
}
- (void)configure{
    _maximumNumberOfSelection = 1;
}
- (void)photoFromCamera{
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;//设置类型为相机
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];//初始化
//        picker.allowsEditing = YES;//设置照片可编辑
        picker.sourceType = sourceType;
        //        picker.showsCameraControls = NO;//默认为YES
        picker.cameraDevice=UIImagePickerControllerCameraDeviceFront;//选择前置摄像头或后置摄像头
        picker.modalPresentationStyle = UIModalPresentationFullScreen;
        [self.ownController presentViewController:picker animated:YES completion:NULL];
        picker.delegate = self;//设置代理
    }
}

- (void)photoFromAlbum{
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
//    imagePicker.allowsEditing = YES;
    imagePicker.modalPresentationStyle = UIModalPresentationFullScreen;
    imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    imagePicker.delegate = self;
    [self.ownController presentViewController:imagePicker animated:YES completion:nil];
}
- (void)dismissImagePickerController{
    [self.ownController dismissViewControllerAnimated:YES completion:NULL];
}
#pragma mark - UIImagePickerControllerDelegate
//从相册选择图片后操作
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    [picker dismissViewControllerAnimated:YES completion:^{
    }];
    //保存原始图片
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    if (self.delegate &&[self.delegate respondsToSelector:@selector(LGKNotePhotoManager:cameraDidSelectImage:)]) {
        [self.delegate LGKNotePhotoManager:self cameraDidSelectImage:image];
    }
}
//取消操作时调用
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [self dismissImagePickerController];
}
@end
